package com.xgkj.diyiketang.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xgkj.diyiketang.R;
import com.xgkj.diyiketang.activity.data.DatiAdapter;
import com.xgkj.diyiketang.data.DatiInfo;
import com.xgkj.diyiketang.http.engine.ShowToastTaskEventListener;
import com.xgkj.diyiketang.task.MyQuestionListTask;
import com.xgkj.diyiketang.util.NoDataUtil;
import com.xgkj.diyiketang.weidget.PullToRefreshListView;

import java.util.ArrayList;
import java.util.List;

public class MyQuestionListActivity extends BaseActivity implements PullToRefreshListView.OnRefreshListener,PullToRefreshListView.OnLoadDataListener{
    private Context mContext;
    private final String TAG = getClass().getSimpleName();
    private RelativeLayout titleLayout;
    private TextView back;
    private TextView gameName;
    private PullToRefreshListView datiListView;
    private NoDataUtil noDataUtil;
    private DatiAdapter mAdapter;
    private int currentPage = 1;
    private int size = 20;
    private String mUserId;
    private int userType;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_question_list);
        mContext = this;
        initView();
    }

    private void initView() {
        if (getIntent() != null){
            mUserId = getIntent().getStringExtra("USERID");
            userType = getIntent().getIntExtra("userType",0);
        }
        View view = findViewById(R.id.content_layout);
        noDataUtil = new NoDataUtil(mContext,view);
        titleLayout = (RelativeLayout) findViewById(R.id.title_layout);
        back = (TextView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        gameName = (TextView) findViewById(R.id.gameName);
        datiListView = (PullToRefreshListView) findViewById(R.id.dati_listView);
        datiListView.init(mContext,true);
        mAdapter = new DatiAdapter(mContext);
        datiListView.setAdapter(mAdapter,true);
        datiListView.setOnRefreshListener(this);
        datiListView.setOnLoadDataListener(this);
        datiListView.doRefresh();
    }


    private void getDatiList(){
        if (currentPage ==0){
            datiListView.setNoNextPage();
            return;
        }

        MyQuestionListTask getDatiListTask = new MyQuestionListTask(mUserId,currentPage,size);
        getDatiListTask.addListener(new TaskListener(mContext));
        getEatShowApp().getTaskManager().addTask(getDatiListTask);
    }

    @Override
    public void onPullRefresh() {

    }

    @Override
    public void onLoadMore() {
        getDatiList();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        getDatiList();
    }


    class TaskListener extends ShowToastTaskEventListener {
        public TaskListener(Context context) {
            super(context);
        }

        @Override
        public void handleTaskCompleted(int taskId, int result, Object data) {
            super.handleTaskCompleted(taskId, result, data);
            if (data != null){
                List<DatiInfo> list = new ArrayList<DatiInfo>();
                list = (List<DatiInfo>) data;
                if (currentPage == 1){
                    datiListView.onRefreshComplete();
                    mAdapter.clearData();
                    datiListView.enableLoadMore();
                }else {
                    datiListView.onLoadDataComplete(true);
                }

                mAdapter.updateData(list, userType);
                currentPage = result;
                if (mAdapter != null && mAdapter.getCount() > 0){
                    noDataUtil.ListViewNoEmpty(datiListView);
                }else {
                    noDataUtil.ListViewEmpty(datiListView,null,null,-1);
                }
            }
        }

        @Override
        public void handleTaskError(int taskId, int errorCode, String errorMsg) {
            super.handleTaskError(taskId, errorCode, errorMsg);
            if (currentPage == 1){
                datiListView.onRefreshComplete();
            }else {
                datiListView.onLoadDataComplete(false);
            }
        }
    }
}
